import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Studentmange {

    static final long serialVersionUID= 7759672884153145099L;
    /*
     * 定义增删改查的方法
     * */
        //调用Student对象
        Student student = new Student();
        //List<Student> list=new ArrayList<>();
        Scanner sc = new Scanner(System.in);

    //定义添加学生方法
        public void addStudent(ArrayList<Student> arr) {
            System.out.println("请输入用户ID:");
            student.setId(sc.nextInt());
            System.out.println("请输入用户姓名：");
            student.setName(sc.next());
            System.out.println("请输入用户年龄：");
            student.setAge(sc.nextInt());
            //System.out.println(student);
            arr.add(student);
            System.out.println("添加成功！");
        }

        //定义删除学生方法
        public void deleteStudent(ArrayList<Student> arr) {
            System.out.println("请输入你要删除的ID：");
            int id = sc.nextInt();
            for (int i = 0; i < arr.size(); i++) {
                Student s = arr.get(i);
                if (s.getId() == id) {
                    arr.remove(i);
                    System.out.println("删除成功！");
                    break;
                } else {
                    System.out.println("输入的ID不存在！");
                }
            }

        }

        //定义修改学生方法
        public void updateStudent(ArrayList<Student> arr) {
            System.out.println("请输入你要修改的ID：");
            int id = sc.nextInt();
            for (int i = 0; i < arr.size(); i++) {
                Student s = arr.get(i);
                if (s.getId() == id) {
//                System.out.println("请输入用户ID:");
//                s.setId(sc.nextInt());
                    System.out.println("请输入修改的用户姓名：");
                    s.setName(sc.next());
                    System.out.println("请输入用户年龄年龄：");
                    s.setAge(sc.nextInt());
                    arr.set(i, s);
                    System.out.println("修改成功！");
                    break;
                } else {
                    System.out.println("输入的ID不存在！");
                }
            }
        }

        //定义查询学生的方法
        public void findAllStudent(ArrayList<Student> arr) {
            if (arr.size() == 0) {
                System.out.println("没有学生信息！");
                return;
            }
            //遍历arr集合输出学生信息
            System.out.println("学号\t姓名\t年龄");
            for (Student s : arr
            ) {
                System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge());

            }
        }


    public void StudentOutputStream(ArrayList<Student> lt){
        ObjectOutputStream oos=null;
        try {
            // 1.创建ObjectOutputStream类型的对象与E:/a.txt文件关联
            oos=new ObjectOutputStream(new FileOutputStream("E:/a.txt",true));

            // 2.将整个传入的lt集合类型的对象写入输出流
            //2.1 遍历集合将对象写入
            for (Student st:lt
            ) {
                oos.writeObject(st);
                System.out.println(st+"写入对象成功！");
            }
            //写入结束标志方便读取（非常重要，如果不写入，在读取的时候无法定位读取结束）；
            oos.writeObject(null);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭输出流
            if(null !=oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void StudentInputStream(ArrayList<Student> lt, String path) {
            ObjectInputStream ois=null;
            Object object=null;
            try {
                // 1.创建ObjectInputStream类型的对象与E:/a.txt文件关联
                ois=new ObjectInputStream(new FileInputStream(path));


                // 2.从输入流中读取对象，初始化Student对象
                while ((object = ois.readObject()) !=null){
                    //System.out.println("object"+object);
                    Student student = (Student) object;
                    //System.out.println(student);
                    lt.add(student);
                }

            /*object=ois.readObject();
            while (object !=null){
                System.out.println(object.toString());
                Student student= (Student) object;
                lt.add(student);
            }*/


            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (null != ois){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

